#!/bin/bash
# 上传mobileprovision文件到服务器上，删除同Name文件，然后导入mobileprovision

set -e

#server_address="okex@okcoinios.f3322.net"

# 上传 mobileprovision文件
# mp_path="$HOME/Desktop/upload.mobileprovision"
#server_dst_path="~/Temp/tmp_mp"
#if [[ $1 ]]; then
#    # 上传 命令行参数里的单个文件
#    mp_path="$1"
#    echo -e "upload file: '${mp_path}' to '${server_dst_path}'\n"
#    scp "${mp_path}" "${server_address}:${server_dst_path}"
#else
#    # 上传 local_source_path目录里的所有mobileprovision文件
#    local_source_path="${HOME}/Desktop/uploadMPFile"
#    find "${local_source_path}" -name "*.mobileprovision" -type f -print |
#    while read tmp_mp_path
#    do
#        echo -e "upload file: '${tmp_mp_path}' to '${server_dst_path}'\n"
#        scp "${tmp_mp_path}" "${server_address}:${server_dst_path}"
#    done
#fi

ws_path="/Users/shaowei/Workspace/Vue/AppSkyVueDemo"
data_path="${ws_path}/_upload_data"

mkdir -p "${data_path}"
rm -rf "${data_path}"
mkdir "${data_path}"

cd "${ws_path}"
npm run build
cp -v index.html "${data_path}"
cp -rv dist "${data_path}"
cp -rv "/Users/shaowei/Workspace/Vue/AppSkyVueDemo/ng" "${data_path}"

# 上传
server_address="root@47.56.113.247"
html_dst_path="/www-data/tmp_upload"
static_dst_path="/www-data/sites/static"
echo -e "upload file: '${data_path}' to '${html_dst_path}'\n"
scp -r "${data_path}" "${server_address}:${html_dst_path}"


# 在远程服务器上执行sync_mp.py
echo "remote run:"
ssh "${server_address}" << eeooff
source ~/.zshrc

set -e

mkdir -p "/www-data/sites/static"
rm -rf "/www-data/sites/static"
mv "/www-data/tmp_upload/_upload_data/ng" "/www-data/sites/static"

html_path="/www-data/sites/html"
mkdir -p "/www-data/sites/html"
rm -rf "/www-data/sites/html"
cp -rv "/www-data/tmp_upload/_upload_data" "/www-data/sites/html"
rm -rf "/www-data/tmp_upload/_upload_data"

chown -R www-data:www-data "/www-data/sites"
ls -l "/www-data/sites/html"
ls -l "/www-data/sites/static"


set +e

exit
eeooff

echo -e "\n*** upload 完成👍***\n"

set +e